<!DOCTYPE HTML>
<html lang="zh-CN">
<head><meta name="generator" content="Hexo 3.9.0">
    <!--Setting-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta http-equiv="Cache-Control" content="no-siteapp">
    <meta http-equiv="Cache-Control" content="no-transform">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta name="apple-mobile-web-app-capable" content="武威涛哥的博客">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no,email=no,adress=no">
    <meta name="browsermode" content="application">
    <meta name="screen-orientation" content="portrait">
    <meta name="theme-version" content="1.2.3">
    <meta name="root" content="/">
    <link rel="dns-prefetch" href="https://jaredtao.github.io">
    <!--SEO-->

    <meta name="keywords" content="Qt,Qml,QtQuick,组件化编程">


    <meta name="description" content="
简介
效果预览
源码中输出中文
Qt本身的国际化
翻译工作
实现动态翻译
加载翻译文件
Qml中切换语言


多国语言版本

简介本文是《Qml组件化编程》系列文章的第四篇，涛哥将教大家，如何...">



<meta name="robots" content="all">
<meta name="google" content="all">
<meta name="googlebot" content="all">
<meta name="verify" content="all">

    <!--Title-->

<title>
    
    Qml组件化编程4-i18n动态国际化 |
    
    武威涛哥的博客
</title>

    <link rel="alternate" href="/atom.xml" title="武威涛哥的博客" type="application/atom+xml">


    <link rel="icon" href="/favicon.ico">

    



<link rel="stylesheet" href="/css/bootstrap.min.css?rev=3.3.7">
<link rel="stylesheet" href="/css/font-awesome.min.css?rev=4.7.0">
<link rel="stylesheet" href="/css/style.css?rev=@@hash">




    
	<div class="hide">
		<script type="text/javascript">
			var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan class='cnzz_stat_icon_1263868967 hide' %3E%3Cscript%20src%3D%22https%3A%2F%2Fs95.cnzz.com%2Fz_stat.php%3Fweb_id%3D1272564536%22%3E%3C%2Fscript%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s19.cnzz.com/z_stat.php%3Fid%3D1263868967%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));
		</script>
	</div>






    

    <script>
        (function(){
            var bp = document.createElement('script');
            var curProtocol = window.location.protocol.split(':')[0];
            if (curProtocol === 'https') {
                bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
            }
            else {
                bp.src = 'http://push.zhanzhang.baidu.com/push.js';
            }
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(bp, s);
        })();
    </script>

</head>

</html>
<!--[if lte IE 8]>
<style>
    html{ font-size: 1em }
</style>
<![endif]-->
<!--[if lte IE 9]>
<div style="ie">你使用的浏览器版本过低，为了你更好的阅读体验，请更新浏览器的版本或者使用其他现代浏览器，比如Chrome、Firefox、Safari等。</div>
<![endif]-->

<body>
    <header class="main-header"  style="background-image:url(http://snippet.shenliyang.com/img/banner.jpg)"  >
    <div class="main-header-box">
        <a class="header-avatar" href="/" title='JaredTao'>
            <img src="/img/avatar.jpg" alt="logo头像" class="img-responsive center-block">
        </a>
        <div class="branding">
        	<!--<h2 class="text-hide">Snippet主题,从未如此简单有趣</h2>-->
            
                <h2> 传承工匠精神 </h2>
            
    	</div>
    </div>
</header>
    <nav class="main-navigation">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="navbar-header"><span class="nav-toggle-button collapsed pull-right" data-toggle="collapse" data-target="#main-menu" id="mnav">
                    <span class="sr-only"></span>
                        <i class="fa fa-bars"></i>
                    </span>
                    <a class="navbar-brand" href="https://jaredtao.github.io">武威涛哥的博客</a>
                </div>
                <div class="collapse navbar-collapse" id="main-menu">
                    <ul class="menu">
                        
                            <li role="presentation" class="text-center">
                                <a href="/"><i class="fa "></i>首页</a>
                            </li>
                        
                            <li role="presentation" class="text-center">
                                <a href="/categories/Qml组件化编程/"><i class="fa "></i>Qml组件化编程</a>
                            </li>
                        
                            <li role="presentation" class="text-center">
                                <a href="/categories/Qml特效/"><i class="fa "></i>Qml特效</a>
                            </li>
                        
                            <li role="presentation" class="text-center">
                                <a href="/categories/Qt进阶之路/"><i class="fa "></i>Qt进阶之路</a>
                            </li>
                        
                            <li role="presentation" class="text-center">
                                <a href="/categories/C-修炼之路/"><i class="fa "></i>C++修炼之路</a>
                            </li>
                        
                            <li role="presentation" class="text-center">
                                <a href="/categories/武威故事/"><i class="fa "></i>武威故事</a>
                            </li>
                        
                            <li role="presentation" class="text-center">
                                <a href="/archives/"><i class="fa "></i>时间轴</a>
                            </li>
                        
                    </ul>
                </div>
            </div>
        </div>
    </div>
</nav>
    <section class="content-wrap">
        <div class="container">
            <div class="row">
                <main class="col-md-8 main-content m-post">
                    <p id="process"></p>
<article class="post">
    <div class="post-head">
        <h1 id="Qml组件化编程4-i18n动态国际化">
            
	            Qml组件化编程4-i18n动态国际化
            
        </h1>
        <div class="post-meta">
    
        <span class="categories-meta fa-wrap">
            <i class="fa fa-folder-open-o"></i>
            <a class="category-link" href="/categories/Qml组件化编程/">Qml组件化编程</a>
        </span>
    

    
        <span class="fa-wrap">
            <i class="fa fa-tags"></i>
            <span class="tags-meta">
                
                    <a class="tag-link" href="/tags/Qml/">Qml</a> <a class="tag-link" href="/tags/Qt/">Qt</a> <a class="tag-link" href="/tags/QtQuick/">QtQuick</a> <a class="tag-link" href="/tags/组件化编程/">组件化编程</a>
                
            </span>
        </span>
    

    
        
        <span class="fa-wrap">
            <i class="fa fa-clock-o"></i>
            <span class="date-meta">2019/05/12</span>
        </span>
        
            <span class="fa-wrap">
                <i class="fa fa-eye"></i>
                <span id="busuanzi_value_page_pv"></span>
            </span>
        
    
</div>
            
            
            <p class="fa fa-exclamation-triangle warning">
                本文于<strong>305</strong>天之前发表，文中内容可能已经过时。
            </p>
        
    </div>
    
    <div id="post-gallery">
        
          <img src="/img/avatar.jpg" alt="gallery-img" class="gallery">
        
    </div>

    <div class="post-body post-content">
        <ul>
<li><a href="#%E7%AE%80%E4%BB%8B">简介</a></li>
<li><a href="#%E6%95%88%E6%9E%9C%E9%A2%84%E8%A7%88">效果预览</a></li>
<li><a href="#%E6%BA%90%E7%A0%81%E4%B8%AD%E8%BE%93%E5%87%BA%E4%B8%AD%E6%96%87">源码中输出中文</a></li>
<li><a href="#qt%E6%9C%AC%E8%BA%AB%E7%9A%84%E5%9B%BD%E9%99%85%E5%8C%96">Qt本身的国际化</a></li>
<li><a href="#%E7%BF%BB%E8%AF%91%E5%B7%A5%E4%BD%9C">翻译工作</a></li>
<li><a href="#%E5%AE%9E%E7%8E%B0%E5%8A%A8%E6%80%81%E7%BF%BB%E8%AF%91">实现动态翻译</a><ul>
<li><a href="#%E5%8A%A0%E8%BD%BD%E7%BF%BB%E8%AF%91%E6%96%87%E4%BB%B6">加载翻译文件</a></li>
<li><a href="#qml%E4%B8%AD%E5%88%87%E6%8D%A2%E8%AF%AD%E8%A8%80">Qml中切换语言</a></li>
</ul>
</li>
<li><a href="#%E5%A4%9A%E5%9B%BD%E8%AF%AD%E8%A8%80%E7%89%88%E6%9C%AC">多国语言版本</a></li>
</ul>
<h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>本文是《Qml组件化编程》系列文章的第四篇，涛哥将教大家，如何在Qml中实现动态国际化。</p>
<p>i18n 是 internationalization(国际化) 的首尾字符加中间的 18 个字符。随着产品越做越大，要推向国际的时候，国际化这一步</p>
<p>是必不可少的。i18n 的方案有很多，这里只讨论在Qt/Qml中的方案。</p>
<p>文章主要发布在<a href="https://jaredtao.github.io">涛哥的博客</a> 和 <a href="https://zhuanlan.zhihu.com/TaoQt" target="_blank" rel="noopener">知乎专栏-涛哥的Qt进阶之路</a>。</p>
<h2 id="效果预览"><a href="#效果预览" class="headerlink" title="效果预览"></a>效果预览</h2><p>看一下最终效果</p>
<p><img src="/images/Qml4/1.gif" alt="预览"></p>
<p>目前支持的语言包括：</p>
<ul>
<li>中文</li>
<li>英文</li>
<li>日文</li>
<li>韩文</li>
<li>法文</li>
<li>俄文</li>
<li>德文</li>
<li>西班牙文</li>
<li>葡萄牙文</li>
<li>意大利文</li>
<li>越南文</li>
<li>阿拉伯文</li>
</ul>
<p>(其中阿拉伯文是从右往左写的，qml默认的处理看起来不完全正确。</p>
<p>涛哥对阿拉伯也不是很熟，就不深入研究了。)</p>
<h2 id="源码中输出中文"><a href="#源码中输出中文" class="headerlink" title="源码中输出中文"></a>源码中输出中文</h2><p>先说一下源码中输出中文的问题。</p>
<p>在文件本身的编码是utf-8的前提下，以下三种方式都可以直接输出中文。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">qInfo() &lt;&lt; u8&quot;山有木兮木有枝，心悦君兮君不知。&quot;;</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">qInfo() &lt;&lt; QStringLiteral(&quot;黄河远上白云间，一片孤城万仞山。&quot;);</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">qInfo() &lt;&lt; QString::fromLocal8Bit(&quot;人生若只如初见，何事秋风悲画扇。&quot;);</span><br></pre></td></tr></table></figure>

<p>u8是c++11标准支持的字符串字面量写法，可以参考<a href="https://zh.cppreference.com/w/cpp/language/string_literal" target="_blank" rel="noopener">https://zh.cppreference.com/w/cpp/language/string_literal</a></p>
<p>QStringLiteral是Qt特有的宏，用来在编译期生成字符串字面量，效果和u8类似。</p>
<p>QString::fromLocal8Bit可以在运行过程中，动态处理中文字符串。</p>
<p>（题外话：有一部分人给涛哥提过 “源码中不该写中文，应该全部做进翻译文件”的思想，但是涛哥在实践中发现这个思想并不能很好地执行。</p>
<p>一方面并没有编译器之类的约束，另一方面也不方便。</p>
<p>涛哥这里提出反问：文件本身是utf8的，utf8就是支持中文的，凭啥就不能写中文了？再者说，我翻译的目标语言正确了，翻译源文是什么语言又有什么关系呢？</p>
<p>俄罗斯人代码写的挺好的，github上就有不少带俄文注释的代码)</p>
<h2 id="Qt本身的国际化"><a href="#Qt本身的国际化" class="headerlink" title="Qt本身的国际化"></a>Qt本身的国际化</h2><p>Qt的国际化, 相关文章太多了，Qt帮助文档也有，涛哥这里列出重点</p>
<ol>
<li>C++代码中的字符串使用QObject::tr()包起来，类本身是QObject的子类时可以省略作用域“QObject::”,直接写tr</li>
<li>qml代码中使用qsTr把字符串包起来</li>
<li>pro文件中添加一句TRANSLATIONS += trans_zh.qs ，这个名字起什么无所谓，关键是‘_zh’要有。</li>
<li>调用lrelease工具,扫描项目并生成trans_zh.qs 文件。这个文件是xml格式的，未经过翻译的，需要为这个文件做一些翻译工作。<br>后面说怎么做翻译。</li>
<li>翻译做好后，调用lupdate工具，生成trans_zh.qm文件。这个文件就是把xml变成了二进制。</li>
<li>将qm文件放在运行路径，或者资源文件里。</li>
<li>切换语言时， Qt/C++代码中使用QTranslater加载qm文件，QCoreApplication卸载旧的QTranslater，并安装新的QTranslater。调用<br>QmlEngine::retranslate函数</li>
</ol>
<p>在5.10以前的版本，Qt是不能直接动态切换语言的，要么重新启动程序，要么把所有的text都set一遍，retranslate是5.10才有的接口。</p>
<p>涛哥这次的方案，以retranslate为主，5.10以前还有各路大神的动态切换语言方案，不在本次文章的讨论范围。</p>
<h2 id="翻译工作"><a href="#翻译工作" class="headerlink" title="翻译工作"></a>翻译工作</h2><p>Qt提供了可视化的工具，即QTDIR/bin路径下的linguist.exe，可以直接打开我们拿到的trans_zh.qs文件。这个工具可以由程序员之外不懂xml的专业翻译人士使用。</p>
<p>涛哥在这里就使用自动化的翻译工具来做这个事情，比如使用 百度翻译API 或 有道翻译API。</p>
<p>为此，涛哥使用go语言写了一个小工具，能接入百度和有道的翻译API，并且能读取Qt的qs文件、完成翻译后再写回qs文件。</p>
<p>小工具已经开源 <a href="https://github.com/jaredtao/Transer" target="_blank" rel="noopener">Qt-Transer</a></p>
<p>当然这种小工具Qt也能做，涛哥使用go纯属兴趣。</p>
<h2 id="实现动态翻译"><a href="#实现动态翻译" class="headerlink" title="实现动态翻译"></a>实现动态翻译</h2><h3 id="加载翻译文件"><a href="#加载翻译文件" class="headerlink" title="加载翻译文件"></a>加载翻译文件</h3><p>需要在Qt/C++代码中实现，并提供给qml调用。这里以中文和英文为例，涛哥直接写在了自定义QQuivkView的代码里。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//TaoView.h</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TaoView</span> :</span> <span class="keyword">public</span> QQuickView</span><br><span class="line">&#123;</span><br><span class="line">    Q_OBJECT</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">explicit</span> <span class="title">TaoView</span><span class="params">(QWindow *parent = <span class="literal">nullptr</span>)</span></span>;</span><br><span class="line">    <span class="function">Q_INVOKABLE <span class="keyword">void</span> <span class="title">reTrans</span><span class="params">(<span class="keyword">const</span> QString &amp;lang)</span></span>;  <span class="comment">//这是通过invokable导出的函数</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> slots:</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    QTranslator m_enTrans;      <span class="comment">//英文的翻译</span></span><br><span class="line">    QTranslator m_zhTrans;      <span class="comment">//中文的翻译</span></span><br><span class="line">    QString m_lang;             <span class="comment">//记录当前语言</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// TaoView.cpp</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"stdafx.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"TaoView.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;QTranslator&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;QQmlEngine&gt;</span></span></span><br><span class="line">TaoView::TaoView(QWindow *parent) : QQuickView(parent)</span><br><span class="line">&#123;</span><br><span class="line">    setFlag(Qt::FramelessWindowHint);</span><br><span class="line">    setResizeMode(SizeRootObjectToView);</span><br><span class="line">    setColor(QColor(Qt::transparent));</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//构造函数直接加载翻译文件</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">bool</span> ok = m_enTrans.load(<span class="string">"trans_en.qm"</span>);</span><br><span class="line">    <span class="keyword">bool</span> ok2 = m_zhTrans.load(<span class="string">"trans_zh.qm"</span>);</span><br><span class="line">    qWarning() &lt;&lt; ok &lt;&lt; ok2;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//默认安装中文</span></span><br><span class="line">    QCoreApplication::installTranslator(&amp;m_zhTrans);</span><br><span class="line">    m_lang = QStringLiteral(<span class="string">"中文简体"</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> TaoView::reTrans(<span class="keyword">const</span> QString &amp;lang)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> (m_lang == lang)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//切换语言</span></span><br><span class="line">    m_lang = lang;</span><br><span class="line">    <span class="keyword">if</span> ( lang == QStringLiteral(<span class="string">"中文简体"</span>))</span><br><span class="line">    &#123;</span><br><span class="line">        QCoreApplication::removeTranslator(&amp;m_enTrans);</span><br><span class="line">        QCoreApplication::installTranslator(&amp;m_zhTrans);</span><br><span class="line">        engine()-&gt;retranslate();</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (lang == <span class="string">"English"</span>) &#123;</span><br><span class="line">        QCoreApplication::removeTranslator(&amp;m_zhTrans);</span><br><span class="line">        QCoreApplication::installTranslator(&amp;m_enTrans);</span><br><span class="line">        engine()-&gt;retranslate();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Qml中切换语言"><a href="#Qml中切换语言" class="headerlink" title="Qml中切换语言"></a>Qml中切换语言</h3><p>和文章3 <a href="https://jaredtao.github.io/2019/05/12/Qml%E7%BB%84%E4%BB%B6%E5%8C%96%E7%BC%96%E7%A8%8B3-%E5%8A%A8%E6%80%81%E5%88%87%E6%8D%A2%E7%9A%AE%E8%82%A4/">Qml组件化编程3-动态切换皮肤</a> 类似, 在标题栏按钮中做一个切换按钮</p>
<figure class="highlight qml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// TitleBar.qml</span></span><br><span class="line"><span class="title">TImageBtn</span> &#123;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">20</span></span><br><span class="line">    <span class="attribute">height</span>: <span class="number">20</span></span><br><span class="line">    <span class="attribute">anchors.verticalCenter</span>: <span class="built_in">parent</span>.verticalCenter</span><br><span class="line">    <span class="attribute">imageUrl</span>: containsMouse ? <span class="string">"qrc:/Image/Window/lang_white.png"</span> : <span class="string">"qrc:/Image/Window/lang_gray.png"</span></span><br><span class="line">    <span class="attribute">onClicked</span>: &#123;</span><br><span class="line">        pop.show()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title">TPopup</span> &#123;</span><br><span class="line">        <span class="attribute">id:</span><span class="string"> pop</span></span><br><span class="line">        <span class="attribute">barColor</span>: gConfig.reserverColor</span><br><span class="line">        <span class="attribute">backgroundWidth</span>: <span class="number">100</span></span><br><span class="line">        <span class="attribute">backgroundHeight</span>: <span class="number">80</span></span><br><span class="line">        <span class="attribute">contentItem</span>: <span class="title">ListView</span> &#123;</span><br><span class="line">            <span class="attribute">id:</span><span class="string"> langListView</span></span><br><span class="line">            <span class="attribute">anchors.fill</span>: <span class="built_in">parent</span></span><br><span class="line">            <span class="attribute">anchors.margins</span>: <span class="number">2</span></span><br><span class="line">            <span class="attribute">model</span>: [<span class="string">"中文简体"</span>, <span class="string">"English"</span>]      <span class="comment">//语言列表，这里不需要qsTr</span></span><br><span class="line">            <span class="attribute">delegate</span>: <span class="title">TTextBtn</span> &#123;</span><br><span class="line">                <span class="attribute">width</span>: langListView.width</span><br><span class="line">                <span class="attribute">height</span>: <span class="number">36</span></span><br><span class="line">                <span class="attribute">text</span>: modelData</span><br><span class="line">                <span class="attribute">color</span>: containsMouse ? <span class="string">"lightgray"</span> : pop.barColor</span><br><span class="line">                <span class="attribute">onClicked</span>: &#123;</span><br><span class="line">                    pop.hide()</span><br><span class="line">                    <span class="comment">//调用Q_INVOKABLE导出的函数，reTrans</span></span><br><span class="line">                    view.reTrans(modelData)</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="多国语言版本"><a href="#多国语言版本" class="headerlink" title="多国语言版本"></a>多国语言版本</h2><p>前面的代码只有中文和英文，已经可以说明问题了。</p>
<p>这里再把最终版本的多国语言的贴出来。</p>
<p>这是头文件</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//TaoView.h</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">pragma</span> once</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;QQuickView&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;memory&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TaoView</span> :</span> <span class="keyword">public</span> QQuickView</span><br><span class="line">&#123;</span><br><span class="line">    Q_OBJECT</span><br><span class="line">    Q_PROPERTY(QStringList languageList READ languageList NOTIFY languageListChanged)</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">explicit</span> <span class="title">TaoView</span><span class="params">(QWindow *parent = <span class="literal">nullptr</span>)</span></span>;</span><br><span class="line">    <span class="function">Q_INVOKABLE <span class="keyword">void</span> <span class="title">reTrans</span><span class="params">(<span class="keyword">const</span> QString &amp;lang)</span></span>;</span><br><span class="line">    <span class="function"><span class="keyword">const</span> QStringList &amp;<span class="title">languageList</span><span class="params">()</span> <span class="keyword">const</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> m_languageList;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">signals:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">reTransed</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">languageListChanged</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    QString m_lang;</span><br><span class="line">    QMap&lt;QString, <span class="built_in">std</span>::<span class="built_in">shared_ptr</span>&lt;QTranslator&gt;&gt; m_transMap;</span><br><span class="line">    QTranslator *m_pLastLang = <span class="literal">nullptr</span>;</span><br><span class="line">    QStringList m_languageList;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>这是cpp</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//TaoView.cpp</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"stdafx.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"TaoView.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;QTranslator&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;QQmlEngine&gt;</span></span></span><br><span class="line">TaoView::TaoView(QWindow *parent) : QQuickView(parent)</span><br><span class="line">&#123;</span><br><span class="line">    setFlag(Qt::FramelessWindowHint);</span><br><span class="line">    setResizeMode(SizeRootObjectToView);</span><br><span class="line">    setColor(QColor(Qt::transparent));</span><br><span class="line"></span><br><span class="line">    m_languageList &lt;&lt; <span class="string">u8"中文简体"</span></span><br><span class="line">                   &lt;&lt; <span class="string">u8"English"</span></span><br><span class="line">                   &lt;&lt; <span class="string">u8"日本語"</span></span><br><span class="line">                   &lt;&lt; <span class="string">u8"한국어"</span></span><br><span class="line">                   &lt;&lt; <span class="string">u8"Français"</span></span><br><span class="line">                   &lt;&lt; <span class="string">u8"Español"</span></span><br><span class="line">                   &lt;&lt; <span class="string">u8"Portugués"</span></span><br><span class="line">                   &lt;&lt; <span class="string">u8"In Italiano"</span></span><br><span class="line">                   &lt;&lt; <span class="string">u8"русский язык"</span></span><br><span class="line">                   &lt;&lt; <span class="string">u8"Tiếng Việt"</span></span><br><span class="line">                   &lt;&lt; <span class="string">u8"Deutsch"</span></span><br><span class="line">                   &lt;&lt; <span class="string">u8" عربي ، "</span>;</span><br><span class="line">    QStringList fileList;</span><br><span class="line">    fileList &lt;&lt; <span class="string">"trans_zh.qm"</span></span><br><span class="line">            &lt;&lt; <span class="string">"trans_en.qm"</span></span><br><span class="line">            &lt;&lt; <span class="string">"trans_ja.qm"</span></span><br><span class="line">            &lt;&lt; <span class="string">"trans_ko.qm"</span></span><br><span class="line">            &lt;&lt; <span class="string">"trans_fr.qm"</span></span><br><span class="line">            &lt;&lt; <span class="string">"trans_es.qm"</span></span><br><span class="line">            &lt;&lt; <span class="string">"trans_pt.qm"</span></span><br><span class="line">            &lt;&lt; <span class="string">"trans_it.qm"</span></span><br><span class="line">            &lt;&lt; <span class="string">"trans_ru.qm"</span></span><br><span class="line">            &lt;&lt; <span class="string">"trans_vi.qm"</span></span><br><span class="line">            &lt;&lt; <span class="string">"trans_de.qm"</span></span><br><span class="line">            &lt;&lt; <span class="string">"trans_ar.qm"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">auto</span> i = <span class="number">0</span>; i &lt; m_languageList.length(); ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">auto</span> trans = <span class="built_in">std</span>::make_shared&lt;QTranslator&gt;();</span><br><span class="line">        <span class="keyword">bool</span> ok = trans-&gt;load(fileList.at(i));</span><br><span class="line">        qWarning() &lt;&lt; m_languageList.at(i) &lt;&lt; fileList.at(i) &lt;&lt; ok;</span><br><span class="line">        m_transMap[m_languageList.at(i)] = trans;</span><br><span class="line">    &#125;</span><br><span class="line">    m_pLastLang = m_transMap[m_languageList.at(<span class="number">0</span>)].get();</span><br><span class="line">    QCoreApplication::installTranslator(m_pLastLang);</span><br><span class="line">    m_lang = m_languageList.at(<span class="number">0</span>);</span><br><span class="line">    <span class="function">emit <span class="title">languageListChanged</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> TaoView::reTrans(<span class="keyword">const</span> QString &amp;lang)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> (m_lang == lang)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    m_lang = lang;</span><br><span class="line"></span><br><span class="line">    QCoreApplication::removeTranslator(m_pLastLang);</span><br><span class="line">    m_pLastLang = m_transMap[lang].get();</span><br><span class="line">    QCoreApplication::installTranslator(m_pLastLang);</span><br><span class="line">    engine()-&gt;retranslate();</span><br><span class="line">    <span class="function">emit <span class="title">reTransed</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>还有qml</p>
<figure class="highlight qml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">....</span><br><span class="line">    <span class="title">TImageBtn</span> &#123;</span><br><span class="line">        <span class="attribute">width</span>: <span class="number">20</span></span><br><span class="line">        <span class="attribute">height</span>: <span class="number">20</span></span><br><span class="line">        <span class="attribute">anchors.verticalCenter</span>: <span class="built_in">parent</span>.verticalCenter</span><br><span class="line">        <span class="attribute">imageUrl</span>: containsMouse ? <span class="string">"qrc:/Image/Window/lang_white.png"</span> : <span class="string">"qrc:/Image/Window/lang_gray.png"</span></span><br><span class="line">        <span class="attribute">onClicked</span>: &#123;</span><br><span class="line">            pop.show()</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="title">TPopup</span> &#123;</span><br><span class="line">            <span class="attribute">id:</span><span class="string"> pop</span></span><br><span class="line">            <span class="attribute">barColor</span>: gConfig.reserverColor</span><br><span class="line">            <span class="attribute">backgroundWidth</span>: <span class="number">100</span></span><br><span class="line">            <span class="attribute">backgroundHeight</span>: <span class="number">400</span></span><br><span class="line">            <span class="attribute">contentItem</span>: <span class="title">ListView</span> &#123;</span><br><span class="line">                <span class="attribute">id:</span><span class="string"> langListView</span></span><br><span class="line">                <span class="attribute">anchors.fill</span>: <span class="built_in">parent</span></span><br><span class="line">                <span class="attribute">anchors.margins</span>: <span class="number">2</span></span><br><span class="line">                <span class="attribute">model</span>: view.languageList        <span class="comment">//这里换成了list属性</span></span><br><span class="line">                <span class="attribute">delegate</span>: <span class="title">TTextBtn</span> &#123;</span><br><span class="line">                    <span class="attribute">width</span>: langListView.width</span><br><span class="line">                    <span class="attribute">height</span>: <span class="number">36</span></span><br><span class="line">                    <span class="attribute">text</span>: modelData</span><br><span class="line">                    <span class="attribute">color</span>: containsMouse ? <span class="string">"lightgray"</span> : pop.barColor</span><br><span class="line">                    <span class="attribute">onClicked</span>: &#123;</span><br><span class="line">                        pop.hide()</span><br><span class="line">                        view.reTrans(modelData)</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">....</span><br></pre></td></tr></table></figure>
    </div>
    
        <div class="reward" ontouchstart>
    <div class="reward-wrap">赏
        <div class="reward-box">
            
                <span class="reward-type">
                    <img class="alipay" src="/img/zhifubao.jpg"><b>支付宝打赏</b>
                </span>
            
            
                <span class="reward-type">
                    <img class="wechat" src="/img/weixin.jpg"><b>微信打赏</b>
                </span>
            
        </div>
    </div>
    <p class="reward-tip">赞赏是不耍流氓的鼓励</p>
</div>


    
    <div class="post-footer">
        <div>
            
                转载声明：文章采用<a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议</a>进行许可, 转载请注明出处 © <a href="https://jaredtao.github.io" target="_blank">武威的涛哥</a>
            
        </div>
        <div>
            
        </div>
    </div>
</article>

<div class="article-nav prev-next-wrap clearfix">
    
        <a href="/2019/05/17/Qml组件化编程5-Qml与C++交互/" class="pre-post btn btn-default" title='Qml组件化编程5-Qml与C++交互'>
            <i class="fa fa-angle-left fa-fw"></i><span class="hidden-lg">上一篇</span>
            <span class="hidden-xs">Qml组件化编程5-Qml与C++交互</span>
        </a>
    
    
        <a href="/2019/05/12/Qml组件化编程3-动态切换皮肤/" class="next-post btn btn-default" title='Qml组件化编程3-动态切换皮肤'>
            <span class="hidden-lg">下一篇</span>
            <span class="hidden-xs">Qml组件化编程3-动态切换皮肤</span><i class="fa fa-angle-right fa-fw"></i>
        </a>
    
</div>


    <div id="comments">
        
	
    <div id="vcomments" class="valine"></div>
    <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="/assets/valine.min.js"></script>

    <script>
        new Valine({
            av: AV,
            el: '#vcomments',
            appId: 'qegD4552u6FU9HoNDvRsonko-gzGzoHsz',
            appKey: 'eitB2FV5n2qyMHcDM7x6EQpt',
            placeholder: '说点什么吧',
            notify: false,
            verify: false,
            avatar: 'mm',
            meta: 'nick,mail'.split(','),
            pageSize: '10',
            path: window.location.pathname,
            lang: 'zh-CN'.toLowerCase()
        })
    </script>


    </div>





                </main>
                
                    <aside id="article-toc" role="navigation" class="col-md-4">
    <div class="widget">
        <h3 class="title">文章目录</h3>
        
            <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#简介"><span class="toc-text">简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#效果预览"><span class="toc-text">效果预览</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#源码中输出中文"><span class="toc-text">源码中输出中文</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Qt本身的国际化"><span class="toc-text">Qt本身的国际化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#翻译工作"><span class="toc-text">翻译工作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#实现动态翻译"><span class="toc-text">实现动态翻译</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#加载翻译文件"><span class="toc-text">加载翻译文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Qml中切换语言"><span class="toc-text">Qml中切换语言</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#多国语言版本"><span class="toc-text">多国语言版本</span></a></li></ol>
        
    </div>
</aside>

                
            </div>
        </div>
    </section>
    <footer class="main-footer">
    <div class="container">
        <div class="row">
        </div>
    </div>
</footer>

<a id="back-to-top" class="icon-btn hide">
	<i class="fa fa-chevron-up"></i>
</a>




    <div class="copyright">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="busuanzi">
    
        访问量:
        <strong id="busuanzi_value_site_pv">
            <i class="fa fa-spinner fa-spin"></i>
        </strong>
        &nbsp; | &nbsp;
        访客数:
        <strong id="busuanzi_value_site_uv">
            <i class="fa fa-spinner fa-spin"></i>
        </strong>
    
</div>

            </div>
            <div class="col-sm-12">
                <span>Copyright &copy;
                    2019
                    
                </span> |
                <span>
                    Powered by <a href="//hexo.io" class="copyright-links" target="_blank" rel="nofollow">Hexo</a>
                </span> |
                <span>
                    Theme by <a href="//github.com/shenliyang/hexo-theme-snippet.git" class="copyright-links" target="_blank" rel="nofollow">Snippet</a>
                </span>
            </div>
        </div>
    </div>
</div>




    <script src="/assets/tagcanvas.min.js?rev=2.9"></script>
    <script>
        var tagOption = {
            textColour: '#444', // 字体颜色
            outlineMethod: 'block', // 选中模式
            outlineColour: '#FFDAB9', // 选中模式的颜色
            interval: 30 || 30, // 动画帧之间的时间间隔，值越大，转动幅度越大
            textHeight: 13,
            outlineRadius: 3,
            freezeActive: true || '', // 选中的标签是否继续滚动
            frontSelect: true || '', // 不选标签云后部的标签
            initial: [0.1, -0.1],
            depth: 0.5,
            decel: 0.95,
            maxSpeed: 0.03,
            reverse: true || '', // 是否反向触发
            fadeIn: 500, // 进入动画时间
            wheelZoom: false || '' // 是否启用鼠标滚轮
        }
        TagCanvas.Start('tag-cloud-3d','',tagOption);
    </script>



    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>


<script src="/js/app.js?rev=@@hash"></script>

</body>
</html>